<?php
declare (strict_types=1);

namespace app\api\controller;

use app\common\tool\Result;
use think\Response;
use app\common\model\Draw as DrawModel;
use think\exception\ValidateException;

class Draw extends Base
{
    public function index(): Response
    {
        $result['account'] = [
            [
                'title' => get_pay_way('bonus'),
                'type' => 'bonus',
                'balance' => $this->user->assets->bonus
            ]
        ];
        $result['method'] = [
            [
                'title' => get_pay_way('bank'),
                'type' => 'bank',
                'icon' => '/static/admin/img/bank.png',
                'bank_name' => $this->user->bank_name,
                'bank_account_name' => $this->user->bank_account_name,
                'bank_number' => $this->user->bank_number,
                'bank_open' => $this->user->bank_open
            ],
            [
                'title' => get_pay_way('wechat'),
                'type' => 'wechat',
                'icon' => '/static/admin/img/wechat.png',
                'wechat_name' => $this->user->wechat_name,
                'wechat_image' => $this->user->wechat_image
            ],
            [
                'title' => get_pay_way('alipay'),
                'type' => 'alipay',
                'icon' => '/static/admin/img/alipay.png',
                'alipay_name' => $this->user->alipay_name,
                'alipay_image' => $this->user->alipay_image
            ]
        ];
        $result['factor'] = [
            'draw_open' => $this->system['draw_open'],
            'draw_times' => $this->system['draw_times'],
            'draw_week' => $this->system['draw_week'],
            'draw_day' => $this->system['draw_day'],
            'draw_min' => $this->system['draw_min'],
            'draw_bs' => $this->system['draw_bs'],
            'draw_cost' => $this->system['draw_cost'],
            'draw_msg' => handleRichText($this->system['draw_msg'])
        ];

        return Result::success('success', $result);
    }

    public function save(): Response
    {
        $param = $this->request->post();
        if (!$this->user->pay_password) {
            return Result::fastReturn(['code' => -4, 'msg' => '请先完善支付密码']);
        }
        try {
            $this->validate($param, 'Draw');
        } catch (ValidateException $e) {
            return Result::fail($e->getError());
        }

        return Result::fastReturn(DrawModel::add($param, $this->user, $this->system));
    }

    public function record(): Response
    {
        return Result::fastReturn(DrawModel::getPageList(['uid' => $this->user->id],
            'id,account,mode,amount,cost,real_amount,status,reject_msg,create_time',
            $this->getPage(), $this->getLimit(), false, [], [], [], [], [], '', ['status_name']));
    }
}